package com.book.book.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.book.book.domain.RulerDO;
import com.book.book.service.RulerService;
import com.book.common.utils.PageUtils;
import com.book.common.utils.Query;
import com.book.common.utils.R;

/**
 * 漂流规则表
 * 
 * @author siyuan
 * @email 894266364@qq.com
 * @date 2018-02-02 11:25:32
 */
 
@Controller
@RequestMapping("/book/ruler")
public class RulerController {
	@Autowired
	private RulerService rulerService;
	
	@GetMapping()
	@RequiresPermissions("book:ruler:ruler")
	String Ruler(){
	    return "book/ruler/ruler";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("book:ruler:ruler")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<RulerDO> rulerList = rulerService.list(query);
		int total = rulerService.count(query);
		PageUtils pageUtils = new PageUtils(rulerList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("book:ruler:add")
	String add(){
	    return "book/ruler/add";
	}

	@GetMapping("/edit/{rulerId}")
	@RequiresPermissions("book:ruler:edit")
	String edit(@PathVariable("rulerId") Integer rulerId,Model model){
		RulerDO ruler = rulerService.get(rulerId);
		model.addAttribute("ruler", ruler);
	    return "book/ruler/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("book:ruler:add")
	public R save( RulerDO ruler){
		if(rulerService.save(ruler)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("book:ruler:edit")
	public R update( RulerDO ruler){
		rulerService.update(ruler);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("book:ruler:remove")
	public R remove( Integer rulerId){
		if(rulerService.remove(rulerId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("book:ruler:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] rulerIds){
		rulerService.batchRemove(rulerIds);
		return R.ok();
	}
	
}
